package com.gomtel.smartdevice.ui.activity.tools;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.clj.fastble.BleManager;
import com.clj.fastble.data.BleDevice;
import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchAppBaseActivity;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.ui.adapter.AdapterStandardGeneral;
import com.gomtel.smartdevice.ui.dialog.ProgressDialog;
import com.gomtel.smartdevice.ui.dialog.ShowDialog;
import com.gomtel.smartdevice.utils.CommonUtils;
import com.gomtel.smartdevice.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/5/28 0028.
 */

public class SearchActivity extends WatchAppBaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.img_btn_black)
    ImageButton img_btn_black;
    @BindView(R.id.search)
    View search;
    @BindView(R.id.search_list)
    ListView search_list;

    private ArrayList<Object> objects = new ArrayList<>();
    private AdapterStandardGeneral adapterStandardGeneral;
    private ProgressDialog progressDialog;

    @Override
    protected void registerEvent(CommonEvent commonevent) {
        switch (commonevent.getCode()) {
            case EventCode.SENDDEVICECONNECTIONCONTENT:
                BleDevice bleDevice = (BleDevice) commonevent.getObject();
                Log.e(TAG, "majorDeviceClass" + bleDevice.getName());
                objects.add(bleDevice);
                adapterStandardGeneral.notifyDataSetChanged();
                progressDialog.dismiss();
                break;
            case EventCode.FINISH_SELECT:
                finish();
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_search;
    }

    @Override
    protected void initClass() {
        progressDialog = ShowDialog.newInstance().showProgressStatus(this, "搜索中...");
        adapterStandardGeneral = new AdapterStandardGeneral(objects, this);
        search_list.setAdapter(adapterStandardGeneral);
    }

    @Override
    protected void initData() {
        getBlueTools();
    }

    @Override
    protected void initView() {
        title_name.setText(getString(R.string.device_search));
    }

    @Override
    protected void initListener() {
        img_btn_black.setOnClickListener(this);
        search.setOnClickListener(this);
        search_list.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_btn_black:
                finish();
                break;
            case R.id.search:
                getBlueTools();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        EventBusUtil.sendEvent(new CommonEvent(EventCode.DEVICECONNECTION, objects.get(i)));
        CommonUtils.showToast(this, "正在连接");
    }

    private void getBlueTools() {
        progressDialog.show();
        EventBusUtil.sendEvent(new CommonEvent(EventCode.DEVICECONNECTIONCONTENT));
    }

    @Override
    protected void onResume() {
        super.onResume();
        showConnectedDevice();
    }

    private void showConnectedDevice() {
        List<BleDevice> deviceList = BleManager.getInstance().getAllConnectedDevice();
        LogUtil.e(TAG, "deviceList : " + deviceList.size());
    }


    @Override
    protected void onDestroy() {
        BleManager.getInstance().cancelScan();
        super.onDestroy();
    }
}
